﻿var MapsGoogle = function () {

    function createInfo(title, content) {
        return '<div id="popup"><h1 class="popup-title">' + title + '</h1><div id="popup-body"><p>' + content + '</p></div></div>';
    }

    var mapGeocoding = function () {

        var map = $("#gmap_geocoding").gmap3({
            map: {
                address: "Công Viên Phần Mềm Quan Trung, Quận 12, TP HCM",
                options: {
                    zoom: 15,
                    mapTypeId: google.maps.MapTypeId.MAP,
                    mapTypeControl: false,
                    navigationControl: true,
                    scrollwheel: true
                }
            }
        });

        var handleAction = function () {
            var text = $.trim($('#gmap_geocoding_address').val());
            $('#gmap_geocoding').gmap3({
                clear: {
                    name: ["marker"],
                    last: true
                }
            });
            $("#gmap_geocoding").gmap3({
                getlatlng: {
                    address: text,
                    callback: function (results) {
                        if (!results) return;
                        $(this).gmap3({
                            marker: {
                                name: "marker",
                                latLng: results[0].geometry.location,
                                tag: "${i}",
                                full: true,
                                options: {
                                    draggable: true
                                },
                                animation: google.maps.Animation.DROP,
                                callback: function (marker) {
                                    if (marker) {
                                        $("#gmap_geocoding").gmap3({
                                            map: {
                                                options: {
                                                    zoom: 15,
                                                    center: results[0].geometry.location,
                                                }
                                            }
                                        });
                                    } else {
                                        alert('Địa Chỉ Không Hợp Lệ');
                                    }
                                }
                            }
                        });
                    }
                }
            });
        };

        $('#gmap_geocoding_btn').click(function (e) {
            e.preventDefault();
            handleAction();
        });

        $("#gmap_geocoding_address").keypress(function (e) {
            var keycode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == '13') {
                e.preventDefault();
                handleAction();
            }
        });

        $("#tab_1_1").on('shown.bs.tab', function () {

            /* Trigger map resize event */
            google.maps.event.trigger(map, 'resize');
        });
    };

    return {
        //main function to initiate map samples
        init: function () {
            mapGeocoding();
        }

    };

}();